package com.hao.wms.mvc;

import com.hao.wms.domain.Department;
import com.hao.wms.query.QueryObject;
import com.hao.wms.service.IDepartmentService;
import com.hao.wms.util.RequiredPermission;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import java.io.IOException;

public class DepartmentAction extends BaseAction{
	private Department department = new Department();
	private QueryObject queryObject = new QueryObject();
	private IDepartmentService departmentService;
	
	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	

	public QueryObject getQueryObject() {
		return queryObject;
	}

	public void setQueryObject(QueryObject queryObject) {
		this.queryObject = queryObject;
	}


	public void setDepartmentService(IDepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	@InputConfig(methodName = "input")
	@RequiredPermission(value="部门列表")
	@Override
	public String execute() throws Exception {
		putContext(PAGERESULT, departmentService.query(queryObject));
		return LIST;
	}
	@RequiredPermission(value="新增/修改部门")
	@Override
	public String input() throws Exception {
		if(department.getId()!=null) {
			department = departmentService.get(department.getId());
		}
		return INPUT;
	}
	@RequiredPermission(value="修改部门")
	public String save() {
		try {
			if(department.getId()!=null) {
                departmentService.update(department);
                addActionMessage("修改成功");
            }else {
                departmentService.save(department);
                addActionMessage("保存成功");
            }
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(e.getMessage());
		}
		return SUCCESS;
	}
	@RequiredPermission(value="删除部门")
	public String delete() throws IOException {
		departmentService.delete(department);
		putResponseText("删除成功");
		return NONE;
	}

}
